
exports.UIFactory = Core.Class.singleton({

    classname: 'UIFactory',

	_progressDialog: null,	//will hold reference to progress dialog box
	_blockView: null,  //a UI block view 


    initialize: function() {
        NgLogD('Inside UIFacgory::initialize()');
	},
    
    getButton: function(frame, image, parent, gradients, text, color) {
    	
        var button = new UI.Button();
        button.setFrame(frame);
        button.setImage(image,UI.State.Normal);
        button.controller = parent;
        button.enable = true;

        if(gradients) {
            button.setGradient(gradients[0],UI.State.Normal);
            button.setGradient(gradients[1],UI.State.Selected);
//		    button.setGradient(gradients[2],UI.State.Disabled);
        }
        
        if(typeof text != 'undefined' && text)
        button.setText(text);
        if(typeof color != 'undefined' && color)
        button.setTextColor(color);
        button.setImageFit(UI.ViewGeometry.FitModes.Stretch);
        return  button;
    },
    
    getAlertBox: function(title, text, choices, delegate, callback)
    {
    	var alertBox = new UI.AlertDialog();
		if(title)
		{
			alertBox.setTitle(title);
		}
		if(text)
		{
			alertBox.setText(text);
		}
		if(choices)
		{
			alertBox.setChoices(choices);
		}
		alertBox.controller = delegate;
		alertBox.onchoice = function(ret)
		{
			alertBox.hide();
			//alertBox.controller._resourceSyncHandlerObj.retrySync();
			if(callback)
			{
				callback();
			}
			alertBox.destroy();
		}.bind(this);
		alertBox.show();
    },
    
    getGenericButton: function(frame, normalImage, highlightedImage, text, textColor, textSize,delegate,gradients) {
    	
        var button = new UI.Button();
        button.setFrame(frame);
        button.setImage(normalImage,UI.State.Normal);
        button.setImage(highlightedImage,UI.State.Pressed);
        button.enable = true;
       
        if(typeof text != 'undefined' && text)
        {
            button.setText(text);
        }
        if(typeof textColor != 'undefined' && textColor)
        {
        	button.setTextColor(textColor);
        }
        if(typeof textSize != 'undefined' && textSize)
        {
        	button.setTextSize(textSize);
        }
        
        if(typeof delegate != 'undefined' && delegate)
        {
        	button.controller = delegate;
        }
   
        if(typeof gradients != 'undefined' && gradients) 
        {
            button.setGradient(gradients[0],UI.State.Normal);
            button.setGradient(gradients[1],UI.State.Pressed);
            button.setGradient(gradients[2],UI.State.Disabled);
        }
        button.setImageFit(UI.ViewGeometry.FitModes.Stretch);
        return  button;
    },
    
	getEditText: function(frame, textSize, attributes, inputType, keyType, placeholder, bgColor) {
    	
        var eText = new UI.EditText();
        eText.setFrame(frame);
        if(textSize > 0)
            eText.setTextSize(textSize);
        if(attributes)
            eText.setAttributes(attributes);
        if(inputType)
            eText.setInputType(inputType);
        if(keyType)
            eText.setEnterKeyType(keyType);
        if(placeholder)
            eText.setPlaceholder(placeholder)
        /*if(bgColor)
            eText.setBackgroundColor(bgColor);
        else
            eText.setBackgroundColor("000000FF");*/
        return eText;
	},
	
    getImage: function(frame, imagePath, scale, gradient, bgColor) {
        var image = new UI.Image();
        image.setFrame(frame);
        image.setImage(imagePath);
        if(scale)
            image.setImageScale(scale);
        if(gradient)
            image.setGradient(gradient);
        if(bgColor)
            image.setBackgroundColor(bgColor);
        image.setImageFit(UI.ViewGeometry.FitModes.Stretch);
        return image;
    },
    
    //label
    getTextArea: function(frame, text, textSize, gravity, tColor, bgColor) {
        var label = new UI.Label();
        label.setFrame(frame);
        label.setTextFont("Spookhouse");
        label.setText(text);
        if(textSize > 0)
            label.setTextSize(textSize);
        if(gravity)
            label.setTextGravity(gravity);
        if(tColor)
            label.setTextColor(tColor);
        else
            label.setTextColor("FF000000");
        if(bgColor)
            label.setBackgroundColor(bgColor);
        else
            label.setBackgroundColor("00FFFFFF");
        return label;
    },
    
    
    getView: function(frame, image, gradient, gravity, scale) {
      var view = new UI.View();
        view.setFrame(frame);
        if(image)
            view.setImage(image);
        if(gradient)
            view.setGradient(gradient);
        if(gravity)
            view.setImageGravity(gravity);
        if(scale)
            view.setImageFit(scale);
        return view;
    },
    
    getWebView: function(frame, url) {
        var webView = new UI.WebView();
		webView.setFrame(frame);
		webView.setBackgroundColor("00000000");
		webView.loadUrl(url);
        return webView;
    },
    
    getCheckBox: function(frame, controller, checked) {
        var checkBox = new UI.CheckBox();
		checkBox.setFrame(frame);
        checkBox.controller = controller;
        if(checked)
            checkBox.setChecked(checked);
		return checkBox;
	},
	
	getDateField: function(frame, attributes, format) {
        var dateField = new UI.DateField();
        dateField.setFrame(frame);
		dateField.setAttributes(attributes);
		dateField.setInputType(UI.EditText.InputTypes.Date);
        dateField.setDateFormat(format);
        return dateField;
	},
	
	
	showProgressHUD: function(msgText) {
	
		if(this._progressDialog == null)  {
			//if not created, create one
			this._progressDialog = new UI.ProgressDialog(); 
		}
		
		if(this._progressDialog != null) {
			if(isValidString(msgText)) {
				//if text is provided, update it
				this._progressDialog.setText(msgText);
			}
			//if already created, show it
			this._progressDialog.show();
		}
    },
	
    hideProgressHUD: function() {
        if (this._progressDialog != null) {
            this._progressDialog.setText("");
            this._progressDialog.hide();
            
            this._progressDialog.destroy();
            this._progressDialog = null;
        }
    },
	
	isProgressHUDShowing :function(){
		
		if(this._progressDialog == null)  
		{
			//if not created, create one
			return false; 
		}
		else
		{
			return true;
		}
	},
	
	showToast: function(msgText) {
		if(isValidString(msgText))
		{
			toastDialog = new UI.Toast();
			toastDialog.setText(msgText);
			toastDialog.show();
		}
	},
	
	showBlockView: function() {
		
		if(this._blockView) {
			this.hideBlockView();
		}
		this._blockView = new UI.View();
		if(this._blockView) {
			this._blockView.setFrame([0 , 0 , GACommonUtils.getScreenWidth() , GACommonUtils.getScreenHeight()]);
			this._blockView.setGradient({
								  "innerShadow":"22000000 200.0 {0.0,0.0}",
								  "gradient":[
											  "33000000 0.000",
											  "33000000 1.000"
											  ]
								  });
			NGWindow.document.addChild(this._blockView);	
		}
	},
		
	hideBlockView: function() {
		if(this._blockView) {
			NGWindow.document.removeChild(this._blockView);
			this._blockView.destroy()
			this._blockView = null;
		}
	},
	
	destroy: function() {
		if(this._progressDialog != null) {
			this._progressDialog.destroy();
			this._progressDialog = null;
		}
		
		if(this._blockView != null)
		{
			delete(this._blockView);
			this._blockView = null;
		}
	}
});